package com.charge.web;

import com.charge.domain.User;
import com.charge.dto.LoginDTO;
import com.charge.service.UserService;
import com.charge.util.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Description :
 * Author : 91658
 * Date : 2018/6/7 11:26
 **/
@Controller
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "")
    public String login(String redirectUrl, HttpServletRequest request) {
        request.setAttribute("redirectUrl", redirectUrl);
        return "/login/login";
    }

    @RequestMapping(value = "/user_login", method = RequestMethod.POST)
    public String userLogin(LoginDTO loginDTO, HttpServletRequest request) {
        User user = new User();
        user.setUserName(loginDTO.getLoginName());
        user.setPassword(loginDTO.getPassword());
        user = userService.checkUser(user);
        if (user == null) {
            return "/login/login";
        }
        HttpSession session = request.getSession(true);
        session.setAttribute("user", user);

        String redirectUrl = loginDTO.getRedirectUrl();
        if (StringUtils.isNotEmpty(redirectUrl)) {
            request.setAttribute("redirectUrl", redirectUrl);
            return "/login/login_redirect";
        } else {
            return "/charge/charge_manage";
        }
    }
}
